package com.neptune.imapp.ui.compose.screen.mainPage

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.rememberPagerState
import com.neptune.imapp.R
import com.neptune.imapp.ui.compose.navigation.RouterConfig
import com.neptune.imapp.ui.compose.screen.mainPage.page.*

@OptIn(ExperimentalPagerApi::class, ExperimentalMaterial3Api::class)
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter", "UnrememberedGetBackStackEntry")
@Composable
fun Main(viewModel: MainViewModel, navController: NavHostController) {
    when (val state = viewModel.state.collectAsState().value) {
        MainViewModel.State.Loading -> {
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .fillMaxHeight()
                    .wrapContentSize(Alignment.Center)
            ) {
                CircularProgressIndicator()
            }
        }
        is MainViewModel.State.Data -> {
            val items = listOf("求职", "概况", "消息", "我的")
            val itemIcon = listOf(
                ImageVector.vectorResource(id = R.drawable.ic_search),
                ImageVector.vectorResource(id = R.drawable.ic_apply),
                ImageVector.vectorResource(id = R.drawable.ic_information),
                ImageVector.vectorResource(id = R.drawable.ic_my)
            )
            val itemNav = listOf(
                RouterConfig.ROUTE_Search,
                RouterConfig.ROUTE_Apply,
                RouterConfig.ROUTE_Information,
                RouterConfig.ROUTE_My
            )
            val pagerState = rememberPagerState(0)
            HorizontalPager(count = 2, state = pagerState, userScrollEnabled = false) { it ->
                when (it) {
                    0 -> {
                        val mainNavController = rememberNavController()
                        Scaffold(
                            bottomBar = {
                                NavigationBar() {
                                    val navBackStackEntry by navController.currentBackStackEntryAsState()
                                    val currentDestination = navBackStackEntry?.destination
                                    items.forEachIndexed { index, s ->
                                        NavigationBarItem(
                                            icon = {
                                                Icon(
                                                    imageVector = itemIcon[index],
                                                    contentDescription = null,
                                                    modifier = Modifier
                                                        .size(25.dp)
                                                )
                                            }, label = {
                                                Text(text = s, fontSize = 15.sp)
                                            },
                                            selected =
                                            currentDestination?.hierarchy?.any { it.route == itemNav[index] } == true,
                                            alwaysShowLabel = true,
                                            onClick = {
                                                mainNavController.navigate(itemNav[index])
                                                {
                                                    popUpTo(navController.graph.findStartDestination().id) {
                                                        saveState = true
                                                    }
                                                    launchSingleTop = true
                                                    restoreState = true
                                                }
                                            }
                                        )
                                    }
                                }
                            }
                        ) {
                            NavHost(
                                navController = mainNavController,
                                startDestination = RouterConfig.ROUTE_Search
                            ) {
                                composable(RouterConfig.ROUTE_Search) {
                                    Search(viewModel)
                                }
                                composable(RouterConfig.ROUTE_Information) {
                                    Information(viewModel, pagerState)
                                }
                                composable(RouterConfig.ROUTE_Apply) {
                                    Apply(state.applies)
                                }
                                composable(RouterConfig.ROUTE_My) {
                                    MySelf(state.user, viewModel.basicViewModel.telephone)
                                }
                            }
                        }
                    }
                    1 -> {
                        Chat(mainViewModel = viewModel, pagerState = pagerState)
                    }
                }
            }
        }
    }
}
